import { createSlice, nanoid } from '@reduxjs/toolkit'

interface IProps {
    text: string;
    desc: string;
    isCenter: string;
}

interface ISchema {
    id: string;
    type: string;
    props: IProps;

}

const data: ISchema[] = [
    {
        id: nanoid(),
        type: 'questionPageTitle',
        props: {
            text: '11',
            desc: '11',
            isCenter: '',
        },
    }
]

const schemaSlice = createSlice({
    name: 'schema',
    initialState: {
        currentIndex: -1,
        currentType: '',
        data: data,
    },
    reducers: {
        updateCurrentIndex(state, action) {
            state.currentIndex = action.payload
        },
        updateCurrentType(state, action) {
            state.currentType = action.payload
        },
        insert(state, action) {
            state.data.push(action.payload)
        },
        del(state, action) { },
        updateSchema(state, action) {
            state.data[state.currentIndex].props.text = action.payload
        },
    }
})


export const { insert, del, updateSchema, updateCurrentType, updateCurrentIndex } = schemaSlice.actions
export default schemaSlice.reducer